import {observable, action} from 'mobx';
import {Tools} from 'weaver-mobile';
import { SearchAdvanced } from 'weaver-mobile-page';
import * as API from '../../api/systemdoc';
import CategoryStore from './CategoryStore';
import DocListStore from './DocListStore';
import * as HistoryApi from '../../api/news';
const getLabel = Tools.getLabel;

export default class SystemDocStore {
  @observable viewMode = 'list';// list/category
  @observable elliVisible = false;
  @observable tabKey = 'all';
  @observable afterSearchMode = ''; //目录模式下搜索后，切换tab仍然要在目录模式
  @observable category = {
    id: '',
    pid: '',
    name: getLabel(30898, '全部文档')
  };
  @observable loading = false;
  
  @observable categoryStore = new CategoryStore({
    systemDocStore: this
  });
  @observable docListStore = new DocListStore({
    systemDocStore : this
  })
  @observable searchAdvancedBasicStore = new SearchAdvanced.Store({ route: '/systemdoc/searchAdvance' }); 
  @observable conditions = []; 
  @observable userid;  //传过来的用户id
  @observable scope;  //当前应用id
  searchType = 5;
  
  // 调整为目录模式或文档模式
  @action
  setViewMode = (viewMode) => {
    this.viewMode = viewMode;
    this.elliVisible = false;
    this.afterSearchMode = viewMode;
    API.saveChooseTab({viewmode:viewMode,tabkey:this.tabKey})
    this.getList();
  }
  setEllivisible = (v) => {
    this.elliVisible = v;
  }
  setScope=(v)=>{
    this.scope = v;
  }
  // 上方的tab选择受控
  setTabKey = (key) => {
    if(this.afterSearchMode === 'category'){
      this.viewMode = 'category';
    }
    //切tab时置空目录浏览按钮中所选项
    if(this.categoryStore.browser && this.categoryStore.browser.valueArray) {
      this.categoryStore.browser.clear();
      this.categoryStore.browser.valueArray = [];
      this.categoryStore.browser.valueArrayCache = [];
      this.categoryStore.browser.selectedKeys = [];
    }
    //保存当前用户选择的tabkey以及viewmode
    API.saveChooseTab({viewmode:this.viewMode,tabkey:key})
    this.tabKey = key;
    //切换tab置空搜索条件
    this.docListStore.searchKey = {};
    this.docListStore.searchPage.searchValue = '';
    this.searchAdvancedBasicStore.form.resetFields();
    //列表模式切换tab加loading
    this.docListStore.docListViewPageStore.clearData();
    this.docListStore.docListViewPageStore.setState({refreshLoading:false});
    this.categoryStore.categoryListViewPageStore.clearData();
    this.categoryStore.categoryListViewPageStore.setState({refreshLoading:false});
    this.categoryStore.category.name = key === 'all'? getLabel(30898, '全部文档') : getLabel(1212, '我的文档');
    this.getList();
  }
  setTabAndView = () =>{
    this.viewMode = 'list';
    this.tabKey = 'all';
  }
  /**
   * 初始化读取当前列表
   */
  init = (userid) => {
    API.getUserAndCategory().then((result) => {
    });
    if(userid){
      this.userid = userid;
      this.setTabAndView();
      this.docListStore.getFirstPage({doccreaterid:this.userid,bySearch : 2});
      this.docListStore.setSearchHistory();
      this.getCondition();
      this.categoryStore.category.name = this.tabKey === 'all'? getLabel(30898, '全部文档') : getLabel(1212, '我的文档');
    }else{
      API.getChooseTab().then((result)=>{
          this.tabKey = (result.tabkey !== '') ? result.tabkey : 'all';
          this.viewMode = (result.viewmode !== '') ? result.viewmode:'list';
          this.getList();
          this.docListStore.setSearchHistory();
          this.getCondition();
          this.categoryStore.category.name = this.tabKey === 'all'? getLabel(30898, '全部文档') : getLabel(1212, '我的文档');
      })
    } 
  }
  getList = () => { 
    if (this.viewMode === 'list' || this.tabKey === 'collect') {
      this.docListStore.getFirstPage();
    } else {
      this.categoryStore.getListWithCategory();
    }
  }

   //获取高级搜索
  getCondition = (params) => {
    this.searchAdvancedBasicStore.getData({
      url: '/api/doc/mobile/systemDoc/searchListCondition', 
      method: 'GET'
    })
  }

  //高级搜索
  doSearchAdvance = (params) =>{
    if (this.viewMode === 'list') {
      this.afterSearchMode = 'list';
      this.docListStore.getFirstPage({...this.searchAdvancedBasicStore.form.formParams,bySearch : 2});
      if(this.searchAdvancedBasicStore.form.formData.docsubject.value!==''){
        HistoryApi.addHistory({searchtext:this.searchAdvancedBasicStore.form.formData.docsubject.value,type:this.searchType}).then((result)=>{
          this.docListStore.getHistory();
        })
      }
    } else { 
      this.viewMode = 'list';
      this.doSearchAdvance();
      this.afterSearchMode = 'category';
    }
  }
}